package com.taorui.ajax;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;

/**
 * Servlet implementation class AjaxServlet
 */
@WebServlet("/AjaxServlet1")
public class AjaxServlet1 extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public AjaxServlet1() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-fgenerated method stub
		response.setContentType("text/html;charset=utf-8");
		Book b1=new Book("一本书", "这就是一本书", "JAVA");
		Book b2=new Book("二本书", "这就是二本书", "PHP");
		String json=JSON.toJSONString(b1);
		System.out.println(json);
		Student stu=new Student();
		stu.setName("小王");
		stu.setAge(20);
		stu.setAddress("中国湖南株洲");
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		Date d=null;
		try{
			d=sdf.parse("1990-2-28");
		}catch (ParseException e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		stu.setBrithday(d);
		List<Book> books=new ArrayList<Book>();
		books.add(b1);
		books.add(b2);
		stu.setBooks(books);
		String s=JSON.toJSONString(stu);
		System.out.println(s);
		response.getWriter().print(s);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
